<?php

namespace App\HttpController\Admin;

use App\Utils\Code;
use EasySwoole\Component\Context\ContextManager;
use EasySwoole\HttpAnnotation\AnnotationTag\Di;
use EasySwoole\HttpAnnotation\AnnotationTag\InjectParamsContext;
use EasySwoole\HttpAnnotation\AnnotationTag\Param;

class AdminUser extends Base
{

    /**
     * @Di(key="adminUser")
     */
    protected $model;

    /**
     * @Di(key="adminMenu")
     */
    protected $adminMenu;

    /**
     * @InjectParamsContext(key="data")
     * @Param(name="account", notEmpty="不能为空")
     * @Param(name="password", notEmpty="不能为空")
     */
    public function login()
    {
        $data = ContextManager::getInstance() -> get('data');
        list($account, $password) = array_values($data);
        $result = $this->model->signAndBackUserInfo($account, $password);
        if ($result) return $this->renderSuccessData($result);
        return $this->renderError(Code::AUTH_IS_ERROR);
    }

    /**
     * @InjectParamsContext(key="data")
     * @Param(name="page_no", optional="", integer="必须为整形")
     * @Param(name="page_size")
     */
    public function list()
    {
        $data = ContextManager::getInstance() -> get('data');

        $result = $this->model->list($data);

        if ($result) return $this -> renderSuccessData($result);

        return $this -> renderError(Code::FAILED_TO_FIND_DATA);
    }

    /**
     * 权限菜单
     */
    public function menuList()
    {
        $where = ['id' => [explode(',', self::$userInfo['menu_ids'] ?? ''), 'in']];
        $field = ['id', 'menu_name', 'path', 'pid', 'sort'];
        $result = $this -> adminMenu -> allList(['where' => $where, 'field' => $field]);
        if ($result) return $this -> renderSuccessData($result);
        return $this -> renderError(Code::FAILED_TO_FIND_DATA);
    }

}